package br.ufmg.ubicomp.droidguide.communication;

import java.util.ArrayList;
import java.util.Collection;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import br.ufmg.ubicomp.droidguide.common.DroidGuideConfigurationManager;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideRemoteService;
import br.ufmg.ubicomp.droidguide.eventservice.enums.ServiceType;

public class EventCommunicationService extends CommunicationService{

	public static final String BASE_URL = DroidGuideConfigurationManager.getInstance().getData("server")+ 
		"service/process?operation=list";
	
	public static Collection<DroidGuideRemoteService> getListService() throws Exception {
		/* Make request */
//		DefaultHttpClient client = new DefaultHttpClient();
//		HttpGet method = new HttpGet(BASE_URL);		
//		HttpResponse response =  client.execute(method);
//		HttpEntity entity = response.getEntity();
//		InputStream inStream = entity.getContent();
//		
//		/* Parse XML */		
//		DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//		Document doc = docBuilder.parse(inStream);
//		// root element.
//		Element root = doc.getDocumentElement();
//		// Entities
		NodeList entities = processRequest(BASE_URL);
		
		Collection<DroidGuideRemoteService> services = getListOfServices(entities);								
		
		return services;
	}

	private static Collection<DroidGuideRemoteService> getListOfServices(
			NodeList entities) {
		Collection<DroidGuideRemoteService> services = new ArrayList<DroidGuideRemoteService>();		
		
		for (int i = 0; i < entities.getLength(); i++) {
			Node entityEntry = entities.item(i);
			
			if (entityEntry instanceof Element) {
				Element entityElement = (Element) entityEntry;
				
				if (entityElement.getAttribute("kind").equals("Service")) {
					DroidGuideRemoteService service = new DroidGuideRemoteService();
					service.setKey(entityElement.getAttribute("key"));
					
					NodeList properties = entityElement.getChildNodes();
					
					for (int j = 0; j < properties.getLength(); j++) {
						Node propertyEntry = properties.item(j);
						
						if (propertyEntry instanceof Element) {
							Element propertyElement = (Element) propertyEntry;
							
							Text textNode = (Text) propertyElement.getFirstChild();
							String text = textNode.getData().trim();
							
							if (propertyElement.getTagName().equals("property")) {
								String attributeName = propertyElement.getAttribute("name");
								if (attributeName.equals("description")) {
									service.setDescription(text);
								} else if (attributeName.equals("type")) {
									if (text.equals("weather")) {
										service.addType(ServiceType.WEATHER);
									} else if (text.equals("tourism")) {
										service.addType(ServiceType.TOURISM);
									} else if (text.equals("traffic")) {
										service.addType(ServiceType.TRAFFIC);
									} else if (text.equals("location")) {
										service.addType(ServiceType.LOCATION);
									} else if (text.equals("business")) {
										service.addType(ServiceType.BUSINESS);
									} else if (text.equals("emergency")) {
										service.addType(ServiceType.EMERGENCY);
									} else if (text.equals("shopping")) {
										service.addType(ServiceType.SHOPPING);
									}
								} else if (attributeName.equals("data")) {
									service.setData(text);
								} else if (attributeName.equals("state")) {
									service.setState(text);
								}
							}
						}
					}
					services.add(service);
				}
			}
		}
		return services;
	}
}
